package com.eight.login.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.aliyun.tea.TeaException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.common.pojo.TbMember;
import com.eight.common.vo.Result;

import com.eight.login.dao.StudentDao;
import com.eight.login.service.StudentService;
import com.eight.login.utils.Demo;
import com.eight.login.vo.ShureCodeVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.PrintStream;
import java.time.LocalDateTime;
import java.util.HashMap;

import java.util.List;
import java.util.Random;
import java.util.UUID;


/**
 * @program: qy8-education1
 * @description:
 * @author: 张亚祥
 * @create: 2023-05-31 20:05
 **/
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private Demo demo ;
    @Override
    public TbMember FindByNameAndpwd(String mobile,String password) {
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        wrapper.eq("password", DigestUtil.sha256Hex(password));
        TbMember  tbMember= studentDao.selectOne(wrapper);
        return tbMember;
    }


    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5t7DnCpdg2SS1agZkkFZ")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("gkoLSTXXQwFQMt0DcCca7ARUP8H5HM");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public  Result generateAndSendVerifyCode(String mobile) throws Exception {
        String code = generateVerifyCode();
        java.util.List<String> args = java.util.Arrays.asList(mobile);
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = StudentServiceImpl.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("卢大佬的博客")
                .setTemplateCode("SMS_460975542")
                .setPhoneNumbers(mobile)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return Result.success(code);
    }

    @Override
    public int installMember(TbMember tbMember) {
        studentDao.insert(tbMember);

        return 0;
    }

    public String generateVerifyCode() {
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000));
    }

    @Override
    public int updateMemberPassword(String id, String mobile, String newPassword) {

        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);
        }
        TbMember tbMember = new TbMember();
        tbMember.setId(id);
        tbMember.setPassword(DigestUtil.sha256Hex(newPassword));
        tbMember.setGmtModified(LocalDateTime.now());
        int i = studentDao.updateById(tbMember);
        return i;
    }

    }


